package app.cloudstringers;

import utils.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import define.Constants;
import define.DefaultValue;
import define.SharedPreference;

public class Convert extends Activity {
	private static final String TAG = Convert.class.getSimpleName();

	private AlertDialog.Builder mDialogLogin, mDialogLogout;
	private AlertDialog.Builder mDialogExit;
	private SharedPreferences.Editor mSpEditor;
	private SharedPreferences mSpSettings;

	public static Context mContextUpload;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_upload);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

		mContextUpload = this;

		Constants.utils = new Utils(this);

		// Initial dialogs & variables
		initialDialogs();
	}

	/**
	 * On clicked method
	 */
	public void onClickUpload(View v) {
		switch (v.getId()) {
		case R.id.ibtn_home_in_upload:
			Intent intent = new Intent(Convert.this, Home.class);
			finish();
			startActivity(intent);
			break;
		case R.id.ibtn_logout_in_upload:
			mDialogLogout.show();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			mDialogExit.show();
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onResume() {
		super.onResume();

		if (Constants.utils.isNetworkOnline()) {
			// Get data from shared preference
			mSpSettings = getSharedPreferences(SharedPreference.PREFS,
					Context.MODE_PRIVATE);
			mSpEditor = mSpSettings.edit();

			// If the connection dialog has shown, dismiss it before show new
			// dialog
			Utils.hideConnectionDialog();
		} else {
			Utils.hideConnectionDialog();
			Utils.showConnectionDialog();
		}
	}

	/**
	 * Initial methods
	 */
	private void initialDialogs() {
		mDialogLogin = new AlertDialog.Builder(this);
		mDialogLogin.setTitle(getString(R.string.title_login));
		mDialogLogin.setCancelable(false);
		mDialogLogin.setMessage(getString(R.string.content_login));
		mDialogLogin.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						// new intent to Status tab
						Intent intent = new Intent(Convert.this,
								StatusTab.class);
						finish();
						startActivity(intent);
					}
				});

		mDialogLogout = new AlertDialog.Builder(this);
		mDialogLogout.setTitle(getString(R.string.title_logout));
		mDialogLogout.setCancelable(false);
		mDialogLogout.setMessage(getString(R.string.content_logout));
		mDialogLogout.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						// set data for shared preferences
						mSpEditor.putString(
								SharedPreference.PREFS_KEY_USER_NAME, "user");
						mSpEditor.putString(
								SharedPreference.PREFS_KEY_GLOBAL_ID,
								DefaultValue.DEFAULT_GLOBAL_ID + "");
						mSpEditor.commit();

						// new intent to login page
						Intent intent = new Intent(Convert.this, Login.class);
						finish();
						startActivity(intent);
					}
				});
		mDialogLogout.setNegativeButton(R.string.no,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});

		mDialogExit = new AlertDialog.Builder(Convert.this);
		mDialogExit.setTitle(R.string.title_exit);
		mDialogExit.setIcon(R.drawable.ic_info_normal);
		mDialogExit.setCancelable(false);
		mDialogExit.setMessage(R.string.content_exit);
		mDialogExit.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						android.os.Process.killProcess(android.os.Process
								.myPid());
					}
				});
		mDialogExit.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
					}
				});
	}
}
